namespace ScrumSprintMonitor.Testbench.Impl
{
	#region #using Directives

	using System;
	using System.Collections.Generic;
	using System.ComponentModel;

	using Infrastructure;
	using Infrastructure.Configuration;

	using Microsoft.Practices.ServiceLocation;

	#endregion

	public class TestConnection : IConnection
	{
		private readonly ICollection<string> buildDefinitionList = new List<string> {
		                                                                            	"Test Workbench Build"
		                                                                            };

		private readonly ICollection<string> iterationPathsList = new List<string> {
		                                                                           	"Test Workbench Sprint"
		                                                                           };

		private readonly ICollection<string> projectList = new List<string> {
		                                                                    	"Test Workbench Project"
		                                                                    };

		private readonly IServiceLocator serviceLocator;

		private bool connected;

		private bool disposed;

		public TestConnection(IServiceLocator serviceLocator)
		{
			this.serviceLocator = serviceLocator;
		}

		#region Implementation of IConnection

		public void Connect()
		{
			if (IsConnected)
			{
				throw new InvalidOperationException("The connection to the server is already established.");
			}

			this.connected = true;

			if (IsConnected)
			{
				OnPropertyChanged("IsConnected");
			}
		}

		public void Disconnect()
		{
			if (IsConnected)
			{
				this.connected = false;

				OnPropertyChanged("IsConnected");
			}
		}

		public IBuildInfo GetBuildInfo(ISprintConfiguration configuration)
		{
			if (configuration == null)
			{
				throw new ArgumentNullException("configuration");
			}

			return this.serviceLocator.GetInstance<IBuildInfo>();
		}

		public string GetSprintName(ISprintConfiguration configuration)
		{
			if (configuration == null)
			{
				throw new ArgumentNullException("configuration");
			}

			return "Test Workbench Sprint";
		}

		public Uri GetWorkItemDisplayUri(IWorkItemAdapter workItem)
		{
			return new Uri("http://scrumsprintmonitor.codeplex.com/");
		}

		public IWorkItemStore GetWorkItemStore(ISprintConfiguration configuration)
		{
			if (configuration == null)
			{
				throw new ArgumentNullException("configuration");
			}
			if (!IsConnected)
			{
				throw new ConnectionException("The connection is not yet established");
			}

			return this.serviceLocator.GetInstance<IWorkItemStore>();
		}

		public ICollection<string> GetProjectList()
		{
			return this.projectList;
		}

		public ICollection<string> GetBuildDefinitionList(string teamProject)
		{
			return this.buildDefinitionList;
		}

		public ICollection<string> GetIterationPaths(string teamProject)
		{
			return this.iterationPathsList;
		}

		public bool IsConnected
		{
			get { return this.connected; }
		}

		public bool IsDisposed
		{
			get { return this.disposed; }
		}

		#endregion

		#region INotifyPropertyChanged members

		public event PropertyChangedEventHandler PropertyChanged;

		private void OnPropertyChanged(string propertyName)
		{
			if (PropertyChanged != null)
			{
				PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
			}
		}

		#endregion

		#region IConnection Members

		public void Dispose()
		{
			this.disposed = true;

			Disconnect();
		}

		#endregion
	}
}